<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/Smart_Backstage/layui/css/layui.css">
    <script src="/Smart_Backstage/layui/layui.js" charset="utf-8"></script>
    <script src="\Smart_Backstage\js\jquery-3.4.1.js"></script>

    <style type="text/css">

        .layui-body{
            background-color: #F0F0F0;
        }

        #doContainer{
            background-color: white;
            border-top: 2px solid #1E9FFF;
            margin: 20px;
            border-radius:10px;
        }

        #tableDiv{
            padding: 25px;
        }


        #daohTitle{
            padding-top: 25px;
            padding-left: 35px;
            padding-bottom: 15px;
            display: inline-block;
        }

    </style>

</head>

<body class="layui-layout-body">

<div class="layui-layout layui-layout-admin">

    <div class="header"></div>

    <div class="left"></div>

    <div class="layui-body">
        <!-- 内容主体区域-->
        <div id="doContainer">
                <span class="layui-breadcrumb" id="daohTitle">
                    <a href="index.html">首页</a>
                    <a href="#">用户管理</a>
                    <a><cite>管理员</cite></a>
                </span>

            <div id="tableDiv">


                <div class="layui-btn-group demoTable">
                    <div id="layerDemo" class="layui-input-inline" >
                        <button class="layui-btn" data-method="notice">添加</button>
                    </div>
                    <button class="layui-btn" data-type="getCheckLength">删除</button>
                    <button class="layui-btn" data-type="isAll">刷新</button>
                </div>

                <table class="layui-table" lay-filter="demo" id="demo" ></table>
            </div>
        </div>
    </div>

    <div class="footer"></div>

</div>



<script id="barDemo" type="text/html">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>

    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script id="checkboxTpl" type="text/html">
    <input type="checkbox" name="lock" value="{{d.id}}" title="激活" lay-filter="lockDemo" {{d.status=="Y"?'checked':''}}>
</script>

<script>

    layui.use(['table','laydate','layer'], function(){
        var table = layui.table,
            $ = layui.jquery,
            layer = layui.layer,
            laydate = layui.laydate,
            form = layui.form;

        //监听表格复选框选择
        table.on('checkbox(demo)', function(obj){
            console.log(obj)
        });

        //自定义密码验证
        form.verify({
            title: function(value){
                if(value.length < 5){
                    return '标题至少得5个字符啊';
                }
            }
            ,pass: [
                /^[\S]{6,12}$/
                ,'密码必须6到12位，且不能出现空格'
            ]
            ,content: function(value){
                layedit.sync(editIndex);
            }
        });

        //自定义密码确认验证
        form.verify({
            //确认密码
            repass: function(value){
                if(value !== $('#passOne').val()){
                    return '两次密码输入不一致';
                }
            }
        });



        //数据的查看，编辑，删除
        table.on('tool(demo)', function(obj){
            var data = obj.data;
            if(obj.event === 'detail'){//查看详情
                //layer.msg('ID：'+ data.id + ' 的查看操作');
                dialog2(data.id);
            } else if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    var arrId = [];
                    arrId.push(data.id);
                    $.ajax({
                        // 编写json格式，设置属性和值
                        url: "/Smart_Backstage/user/del",
                        contentType: "application/json;charset=UTF-8",
                        data: JSON.stringify(arrId),
                        dataType: "json",
                        type: "post",
                        success: function (data) {
                            obj.del();
                            layer.close(index);
                        }
                    })

                });
            } else if(obj.event === 'edit'){
                dialog(data.id,data.name,data.phone,data.email,showTime(data.birthday));
            }
        });

        //渲染table
        render();

        function render(){

            table.render({
                elem: '#demo'
                ,url: '/Smart_Backstage/user/findAllMerchant'
                ,page:true
                ,cols: [[ //标题栏
                    {type: 'checkbox', fixed: 'left'}
                    ,{field: 'id', title: 'ID', width: 80, sort: true}
                    ,{field: 'name', title: '用户名', width: 100}
                    ,{field: 'phone', title: '手机号', minWidth: 100}
                    ,{field: 'email', title: '邮箱', minWidth: 100}
                    ,{field: 'sex', title: '性别', width: 80}
                    ,{field: 'birthday', title: '生日', width: 250,templet:function(d){return showTime(d.birthday);}}
                    ,{field: 'status', title: '是否激活', width: 100,templet: '#checkboxTpl', unresize: true}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:250}
                ]]
                ,parseData: function(pageInfo){ //res 即为原始返回的数据
                    return {
                        "code": 0, //解析接口状态
                        "msg": '内容为空', //解析提示文本
                        "count": pageInfo.total, //解析数据长度
                        "data": pageInfo.list //解析数据列表
                    };
                }
            });
        }

        //激活监听
        form.on('checkbox(lockDemo)', function(obj){
            $.ajax({
                // 编写json格式，设置属性和值
                url: "/Smart_Backstage/user/lock",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify({id:parseInt(this.value),flag:obj.elem.checked}),
                dataType: "json",
                type: "post",
                success: function (data) {
                    if(data==1){
                        layer.tips(obj.value + '：'+ obj.elem.checked, obj.othis);
                    }
                }
            })
        });


        //时间初始化
        function showTime(time){
            var date = new Date(time);
            var year = date.getFullYear();
            var month = date.getMonth();
            var day = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (day >= 0 && day <= 9) {
                day = "0" + day;
            }
            return year+'-'+month+'-'+day;
        }


        //触发事件
        var active = {
            notice: function(){//创建用户
                dialog();
            },getCheckLength: function(){ //选中删除
                var checkStatus = table.checkStatus('demo')
                    ,data = checkStatus.data;
                if(data.length==0){
                    layer.msg("没有选中内容");
                }else{
                    var arrID = [];
                    for(var i = 0;i<data.length;i++){
                        arrID.push(data[i].id)
                    }
                    $.ajax({
                        // 编写json格式，设置属性和值
                        url: "/Smart_Backstage/user/del",
                        contentType: "application/json;charset=UTF-8",
                        data: JSON.stringify(arrID),
                        dataType: "json",
                        type: "post",
                        success: function (data) {
                            if(data == 1){
                                render();
                            }
                        }
                    })
                }
            }
            ,isAll: function(){ //刷新
                render();
            }
        };

        //添加和编辑的dialog
        function dialog(id,name,phone,email,birthday){
            if(name == undefined){
                id = "";
                name = "";
                phone = "";
                email = "";
                birthday = "";
            }
            layer.open({
                type: 1
                ,title: false //不显示标题栏
                ,closeBtn: false
                ,area: '750px;'
                ,shade: 0.8
                ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
                ,btnAlign: 'c'
                ,moveType: 1 //拖拽模式，0或者1
                ,content:
                    '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">' +
                    '<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;margin-bottom: 20px; margin-bottom: 40px;" >\n' +
                    '  <legend>编辑用户信息</legend>\n' +
                    '</fieldset>' +
                    '<form class="layui-form" id="UserForm">\n' +
                    '<input type="text" name="id" value="'+id+'" class="layui-input" style="display: none;" >\n' +
                    '<input type="text" name="isMerchant" value="1" class="layui-input" style="display: none;" >\n' +
                    '     <div class="layui-form-item">\n' +
                    '         <label class="layui-form-label">用户名称</label>\n' +
                    '         <div class="layui-input-block">\n' +
                    '             <input type="text" name="name" value="'+name+'" placeholder="请输入用户名" autocomplete="off" lay-verify="required" lay-reqtext="用户名是必填项，岂能为空？" class="layui-input">\n' +
                    '         </div>\n' +
                    '     </div>\n' +
                    '     <div class="layui-form-item">\n' +
                    '         <label class="layui-form-label">密码</label>\n' +
                    '         <div class="layui-input-block">\n' +
                    '             <input name="password" class="layui-input" type="password" id="passOne" placeholder="请输入密码" autocomplete="off" lay-verify="pass">\n' +
                    '         </div>\n' +
                    '     </div>\n' +
                    '     <div class="layui-form-item">\n' +
                    '         <label class="layui-form-label">确定密码</label>\n' +
                    '         <div class="layui-input-block">\n' +
                    '             <input class="layui-input" type="password" placeholder="请再次输入密码" autocomplete="off" lay-verify="repass">\n' +
                    '         </div>\n' +
                    '     </div>\n' +
                    '     <div class="layui-form-item">\n' +
                    '         <label class="layui-form-label">手机号</label>\n' +
                    '         <div class="layui-input-block">\n' +
                    '             <input type="text" name="phone" value="'+phone+'" placeholder="请输入手机号" autocomplete="off" lay-verify="required|phone" class="layui-input" >\n' +
                    '         </div>\n' +
                    '     </div>\n' +
                    '     <div class="layui-form-item">\n' +
                    '         <label class="layui-form-label">邮箱</label>\n' +
                    '         <div class="layui-input-block">\n' +
                    '             <input type="text" name="email" value="'+email+'"placeholder="请输入邮箱" autocomplete="off" lay-verify="email" class="layui-input">\n' +
                    '         </div>\n' +
                    '     </div>\n' +
                    '     <div class="layui-form-item">\n' +
                    '         <label class="layui-form-label">注册时间</label>\n' +
                    '         <div class="layui-input-block">\n' +
                    '             <input name="birthday" class="layui-input" value="'+birthday+'"  id="date" type="text" placeholder="yyyy-MM-dd" autocomplete="off" lay-verify="date">\n' +
                    '         </div>\n' +
                    '     </div>\n' +
                    '     <div class="layui-form-item">\n' +
                    '         <label class="layui-form-label">性别</label>\n' +
                    '         <div class="layui-input-block">\n' +
                    '             <input name="sex" title="男" type="radio" checked="" value="男">\n' +
                    '             <input name="sex" title="女" type="radio" value="女">\n' +
                    '     </div>\n' +
                    '     <div class="layui-form-item">\n' +
                    '         <label class="layui-form-label">是否激活</label>\n' +
                    '         <div class="layui-input-block">\n' +
                    '             <input name="lock" type="checkbox" checked="" lay-skin="switch" lay-text="ON|OFF">\n' +
                    '         </div>\n' +
                    '         </div>' +
                    '     </div>\n' +
                    '     <div class="layui-form-item">\n' +
                    '         <div class="layui-input-block" style=" margin-top: 40px; padding-left: 100px;">\n' +
                    '             <button class="layui-btn" lay-submit lay-filter="formDemo" id="submit">立即提交</button>\n' +
                    '             <button type="reset" class="layui-btn layui-btn-primary">重置</button>\n' +
                    '             <button class="layui-btn layui-btn-primary" id="cancel">取消</button>\n' +
                    '         </div>\n' +
                    '     </div> ' +
                    '</form>\n' +
                    '</div>'
                ,success: function(layero){

                    //开启公历节日
                    laydate.render({
                        elem: '#date'
                        ,calendar: true

                    });

                    form.render('radio')
                    form.render('checkbox')

                    //监听提交
                    form.on('submit(formDemo)', function(data){
                        //alert(JSON.stringify(data.field));


                        $.ajax({
                            // 编写json格式，设置属性和值
                            url: "/Smart_Backstage/user/register",
                            contentType: "application/json;charset=UTF-8",
                            data: JSON.stringify(data.field),
                            dataType: "json",
                            type: "post",
                            success: function (data) {
                                if(data.result == 1){
                                    layer.closeAll();
                                    render();
                                    layer.msg('用户添加成功！', {
                                        time: 5000, //5s后自动关闭
                                        btn: ['好的']
                                    });
                                }else{
                                    layer.msg("手机号或邮箱已注册过，如忘记密码请前往修改密码。", {
                                        time: 5000, //5s后自动关闭
                                        btn: ['好的']
                                    });
                                }
                            }
                        })


                        return false;
                    });


                    $("#cancel").on("click",function(){
                        layer.close(layer.index);
                    })

                }

            });
        }


        //查看的dialog
        function dialog2(id_){

            layer.open({
                type: 1
                ,title: false //不显示标题栏
                ,closeBtn: false
                ,area: '950px;'
                ,shade: 0.8
                //,id: 'LAY_layuipro' //设定一个id，防止重复弹出
                ,btn: ['确定']
                ,btnAlign: 'c'
                ,content:
                    '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">' +
                    '    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;margin-bottom: 20px; margin-bottom: 40px;" >\n' +
                    '        <legend>查看用户信息</legend>\n' +
                    '    </fieldset>' +
                    '    <table class="layui-hide" id="userMessageId"></table>' +
                    '</div>'
                ,success: function(layero){


                    //展示已知数据
                    table.render({
                        elem: '#userMessageId'
                        ,url: '/Smart_Backstage/user/findUserById'
                        ,page:false
                        ,where:{UserId:id_}
                        , cols: [[ //标题栏
                            {field: 'id', title: 'ID', width: 50, sort: true}
                            , {field: 'name', title: '用户名', width: 100}
                            , {field: 'phone', title: '手机号', minWidth: 120}
                            , {field: 'email', title: '邮箱', minWidth: 200}
                            , {field: 'sex', title: '性别', width: 50}
                            , {field: 'registrationTime', title: '注册时间', width: 120,templet:function(d){return showTime(d.registrationTime);}}
                            , {field: 'status', title: '是否激活', width: 50, sort: true}
                            , {field: 'birthday', title: '生日', width: 120,templet:function(d){return showTime(d.birthday);}}
                        ]]
                        ,parseData: function(res){ //res 即为原始返回的数据
                            return {
                                "code": 0, //解析接口状态
                                "msg": '内容为空', //解析提示文本
                                "count": 10, //解析数据长度
                                "data": res //解析数据列表
                            };
                        }
                    })



                    var btn = layero.find('.layui-layer-btn');
                    btn.find('.layui-layer-btn0').on("click",function(){

                    })

                }

            });
        }



        $('#layerDemo .layui-btn').on('click', function(){
            var othis = $(this), method = othis.data('method');
            active[method] ? active[method].call(this, othis) : '';
        });

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


    });
</script>

<script>

    $(function(){
        $('.header').load('header.html');
        $('.left').load('aside.html');
        $('.footer').load('footer.html');
    })

    //JavaScript代码区域
    $("#content") .load("intoIndex",function () {

        layui.use('element', function(){
            var element = layui.element;

        });

    });
</script>


</body>
</html>